const records = require("../models/records")
let user = require("../models/userDetail")
let errorBook = require("../models/errorbook")

var express = require('express');
var router = express.Router();

const formidable = require('formidable');

//返回图片
router.post('/', (req, res, next) => {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
        if (err) {
            res.json({ 'state': false })
        } else {
            records.create({
                menu: fields.menu,
                seq: fields.seq,
                attachment: fields.attachment,
                question_ID: fields.question_ID,
                point_ID: fields.point_ID,
                UUID: fields.UUID,
                stu_answer: fields.stu_answer,
                result: fields.result,
                time: new Date(fields.time)
            }, (error, doc) => {
                if (error) {
                    res.json({ 'state': false, 'msg': error });
                } else {
                    // let udpate_record = () => {
                    //     return new Promise(function (resolve, reject) {
                    //         if (fields.attachment == true && fields.updateProcess) {
                    //             let menu_idx = -1

                    //             let lesson = fields.menu.split(",")[0]

                    //             if (lesson == "js") menu_idx = 0
                    //             else if (lesson == "wx") menu_idx = 1
                    //             else if (lesson == "ydhlw") menu_idx = 2

                    //             // 下面这段代码用于更新学习进度，已废弃
                    //             let menu_temp = ""
                    //             if(fields.lastOrNot == true) {
                    //                 menu_temp = fields.nextMenu
                    //             } else if (fields.lastOrNot == false) {
                    //                 menu_temp = fields.menu
                    //             }

                    //             user.findOne({
                    //                 UUID: fields.UUID
                    //             }, function (error, one_user) {
                    //                 if (error) {
                    //                     res.json({ 'state': false, 'msg': error });
                    //                 } else {
                    //                     let new_process = one_user.process
                    //                     new_process[menu_idx] = [menu_temp, fields.seq, fields.point_ID]
                    //                     user.updateOne({
                    //                         UUID: fields.UUID
                    //                     }, {
                    //                         $set: {
                    //                             process: new_process
                    //                         }
                    //                     }, function (err, doc) {
                    //                         if (err) {
                    //                             res.json({ 'state': false, 'msg': err });
                    //                         } else {
                    //                             resolve()
                    //                         }

                    //                     })
                    //                 }
                    //             });
                    //         } else {
                    //             resolve()
                    //         }
                    //     })
                    // }


                    let error_record = () => {
                        if (fields.result == false) {
                            let temp_id = ""
                            if(fields.attachment == true) temp_id = fields.point_ID
                            else if (fields.attachment == false) temp_id = fields.question_ID

                            errorBook.create({
                                attachment: fields.attachment,
                                question_ID: temp_id,
                                UUID: fields.UUID
                            }, (error, doc) => {
                                if (error) {
                                    res.json({ 'state': false, 'msg': error });
                                } else {
                                    res.json({ 'state': true, 'msg': "ok" });
                                    return
                                }
                            })
                        } else if (fields.result == true) {
                            res.json({ 'state': true, 'msg': "ok" });
                            return
                        }
                    }

                    error_record()

                }
            })

        }

    });

});

module.exports = router;